package tt.dz.egobus.servicemanager.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.egobus.api.usercenter.user.UserInfoBack;
import tt.dz.egobus.base.dao.BaseDao;
import tt.dz.egobus.servicemanager.base.service.AbstractExtendService;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;
import tt.dz.egobus.servicemanager.dao.DriverRegisterBusRelativeDao;
import tt.dz.egobus.servicemanager.entities.BusinessExceptionAlert;
import tt.dz.egobus.servicemanager.entities.DriverRegisterBusRelative;
import tt.dz.egobus.servicemanager.service.BusRegisterServiceService;
import tt.dz.egobus.servicemanager.service.BusService;
import tt.dz.egobus.servicemanager.service.DriverRegisterBusRelativeService;
import tt.dz.egobus.servicemanager.vo.BusinessFinishVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus.BUS_ALIVE_DRIVER_RELATIVE;

/**
 * @author Anson
 * @date 17-3-23
 * @name 司机绑定车辆服务
 * @since 1.0.2
 */
@Service("driverRegisterBusRelativeServiceImpl")
public class DriverRegisterBusRelativeServiceImpl extends AbstractExtendService<DriverRegisterBusRelative, String>
        implements DriverRegisterBusRelativeService {

    @Autowired
    @Qualifier("driverRegisterBusRelativeDaoImpl")
    protected DriverRegisterBusRelativeDao dao;

    @Autowired
    @Qualifier("busRegisterServiceServiceImpl")
    protected BusRegisterServiceService busRegisterServiceService;

    @Autowired
    @Qualifier("busServiceImpl")
    protected BusService busService;

    @Override
    protected BaseDao<DriverRegisterBusRelative, String> getSelfDao() {
        return this.dao;
    }


    @Override
    @Transactional
    public boolean active(String busId, UserInfoBack driverInfo) {
        DriverRegisterBusRelative pojo = new DriverRegisterBusRelative();
        pojo.setIsValidate(true);
        pojo.setAbleActive();
        pojo.setBusId(busId);
        pojo.setDriverId(driverInfo.getUserId());
        pojo.setDriverName(driverInfo.getRealName());
        pojo.setPhoneNumber(driverInfo.getPhoneNumber());
        pojo.setIdCard(driverInfo.getIdCard());
        pojo.setCreateTime(this.getCurrentDbDate());
        try {
            //同步车的状态 -->使用
            boolean isDone = busService.updateBusUseStatus(busId);
            if (isDone) {
                this.dao.save(pojo);
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }


    @Override
    @Transactional
    public boolean down(DriverRegisterBusRelative relative) {
        relative.setEndTime(this.getCurrentDbDate());
        relative.setAbleDown();
        relative.setIsValidate(false);
        try {
            //同步车的状态-->未使用
            boolean isDone = busService.updateBusUnUseStatus(relative.getBusId());
            if (isDone) {
                this.dao.update(relative);
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    @Transactional
    public List<DriverRegisterBusRelative> queryValidateCarDriverBind(String driverId) {
        if (StringUtils.isEmpty(driverId)) {
            return new ArrayList<DriverRegisterBusRelative>(0);
        }
        //检查当前是否存在已经绑定的关系
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("eq#driverId", driverId);
        param.put("eq#isValidate", true);
        List<DriverRegisterBusRelative> list = this.list(param);
        return list == null ? new ArrayList<DriverRegisterBusRelative>(0) : list;
    }

    @Override
    @Transactional
    public boolean doUpdateBeginBusiness(DriverRegisterBusRelative relative, String businessId) {
        relative.setAbleBeginBusiness();
        try {
            this.dao.update(relative);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    @Transactional
    public int[] getBusBusinessCodes(String driverId) {
        List<DriverRegisterBusRelative> driverRegisterBusRelatives = this.queryValidateCarDriverBind(driverId);
        if (driverRegisterBusRelatives == null || driverRegisterBusRelatives.size() != 1) {
            return new int[0];
        }
        DriverRegisterBusRelative relative = driverRegisterBusRelatives.get(0);
        if (relative == null || relative.getBusId() == null) {
            return new int[0];
        }
        this.busRegisterServiceService.queryBusinessCodeByBusId(relative.getBusId());
        return new int[0];
    }

    @Override
    @Transactional
    public boolean doBusiness(String relativeId) {
        DriverRegisterBusRelative relative = this.dao.get(relativeId);
        if (relative == null || !relative.assertBeginBusiness() || !relative.isValidate()) {
            return false;
        }
        relative.setAbleBusiness();
        try {
            this.dao.update(relative);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    @Transactional
    public boolean doFinish(DriverRegisterBusRelative relative, BusinessFinishVo vo) {
        if (relative == null || !relative.assertBusiness() || !relative.isValidate()) {
            return false;
        }
        relative.setAbleActive();
        try {
            this.dao.update(relative);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    @Override
    @Transactional
    public boolean doUserCancel(DriverRegisterBusRelative relative) {
        if (relative == null) {
            return false;
        }
        relative.setAbleActive();
        try {
            this.dao.update(relative);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean doSaveException(DriverRegisterBusRelative relative, BusinessExceptionAlert alert) {
        if (relative == null) {
            return false;
        }
        //解绑
        relative.setIsValidate(false);
        relative.setEndTime(this.getCurrentDbDate());
        relative.setAbleDown();
        boolean isDone = true;
        if (alert.assertBus()) {
            //当前上报是非车辆异常,则当前车辆状态为异常
            isDone = busService.updateBusExceptionStatus(relative.getBusId());
        } else {
            //当前上报是非车辆异常
            //同步车的状态-->未使用
            isDone = busService.updateBusUnUseStatus(relative.getBusId());
        }
        if (isDone) {
            this.dao.update(relative);
        }
        return isDone;
    }

    @Override
    @Transactional
    public int deleteDriverRelativeByBusId(String busId) {
        if (StringUtils.isBlank(busId)) {
            return 0;
        }
        return this.dao.deleteDriverRelativeByBusId(busId);
    }

    @Override
    @Transactional
    public void validateDriverelative(String busId, boolean isFilterValidate) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eq#busId", busId);
        if (isFilterValidate) {
            params.put("eq#isValidate", true);
        }
        int count = this.dao.count(params);
        if (count > 0) {
            throw new BusinessException(BUS_ALIVE_DRIVER_RELATIVE.getCodeStatus());
        }
    }


    /**
     * @param busId
     * @param driverId
     * @author Anson
     * @date 17-3-23
     * @name 是否当前司机与车存在有效绑定关系
     * @since 1.0.2
     */
    @Override
    @Transactional
    public boolean isBindDriverAndCar(String busId, String driverId) {
        //检查当前是否存在已经绑定的关系
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("eq#busId", busId);
        param.put("eq#driverId", driverId);
        param.put("eq#isValidate", true);
        int count = this.dao.count(param);
        return count > 0;
    }

    /**
     * @param busId
     * @author Anson
     * @date 17-3-23
     * @name 是否车存在有效绑定关系
     * @since 1.0.2
     */
    @Override
    @Transactional
    public boolean isValidateBindCar(String busId) {
        //检查当前是否存在已经绑定的关系
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("eq#busId", busId);
        param.put("eq#isValidate", true);
        int count = this.dao.count(param);
        return count > 0;
    }

    /**
     * @param driverId
     * @author Anson
     * @date 17-3-23
     * @name 是否司机存在有效绑定关系
     * @since 1.0.2
     */
    @Override
    @Transactional
    public boolean isValidateBindDriver(String driverId) {
        //检查当前是否存在已经绑定的关系
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("eq#driverId", driverId);
        param.put("eq#isValidate", true);
        int count = this.dao.count(param);
        return count > 0;
    }


}
